package com.icoding.trangphuc

import grails.transaction.Transactional

import com.icoding.trangphuc.utils.Encryption

@Transactional(readOnly = true)
class LoginController {
	
	def loginAdmin ={
		try {
			def listRole ;
			def account = Account.findWhere(username:params['username'],password:Encryption.md5(params['password']))
			if(account !=null){
				listRole= account.roles.asList()
			}
			if(listRole != null){
				if(checkRole(listRole, "administrator")){
					session["user"] = account;
					redirect(uri:'/admin/cat/1')
				}
				else{
					redirect(uri:'/admin/login')
				}
			}
			else{
				redirect(uri:'/admin/login')
			}
		} catch (Exception e) {
			e.printStackTrace()
		}
	}

	def logout = {
		session["user"].invalidate();
	}

	boolean checkRole(def listRole,String roleName){
		boolean flag = false
		for(r in listRole){
			if(r.name==roleName){
				return true
				break
			}
		}
		return flag
	}
}
